/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-04 16:17:43
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-13 11:27:49
 */
// pages/address/index.js
import {
  userAPI
} from './../../api/user'
import {
  returnStatus,
  returnStatusToast,
} from './../../utils/index'
var appInst =  getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    showIntr: false,
    showGetGoods:false,
    addressList:[]

    
  },
  // 选择地址
  chooseAddress({currentTarget:{dataset:info}}){
    if(this.data.type){
      const eventChannel = this.getOpenerEventChannel()
      eventChannel.emit('getAddress', info)
      wx.navigateBack()
    }
  },
  removeAddress({target,detail}) { 
    this.addressId = target.dataset.addressid
    this.instance = detail.instance
    this.setData({showIntr:true})
  },
  confirmDel(){
    userAPI.handleCustomerAddress({
      method:'delete',
      addressId:this.addressId
    }).then(async _=>{
      this.instance.close();
      await returnStatus(_)
      this.fetchAddress()
    })
  },
  insertAddress(){
    const _this = this
    wx.navigateTo({
      url: '/pages/address/edit/index',
      events: {
        async refreshAddress({data}){
          //重新获取地址信息
          _this.fetchAddress()
        }
      },
      fail: () => {},
      complete: () => {
      }
    });
      
  },
  // 请求地址数据
  fetchAddress(){
    userAPI.queryCustomerAddress().then(async _=>{
      const res = await returnStatus(_)
      if(res){
        this.setData({
          addressList:res
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({type}) {
    // 首次加载地址列表
    appInst.setTitle(type?'选择地址':'地址管理')
    this.setData({type})
    this.fetchAddress()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})